﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DTBuilderLib.VisualHierarchyClasses;
using System.IO;

namespace t_DTBuilderLib
{
    /// <summary>
    /// Summary description for t_VisualSingleImage
    /// </summary>
    [TestClass]
    public class t_VisualSingleImage
    {
        public t_VisualSingleImage()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void BasicLoadofVisualImageFile()
        {
            FileInfo f = new FileInfo("..\\..\\..\\t_DTBuilderLib\\file1.jpg");
            Assert.IsTrue(f.Exists, "Image file not found");
            VisualSingleImage_Accessor c = new VisualSingleImage_Accessor(f, 600);
            Assert.IsTrue(c.Height == 7.5, "Height of image is not right"); // 1 less due ot margins!
            Assert.IsTrue(c.Width == 10.0, "Width of image is not right");

            Assert.IsTrue(c.XPosition == 0.0F, "Initial position is not right");
            Assert.IsTrue(c.YPosition == 0.0F, "Initial position is not right");

            c.XPosition = 1.0F;
            c.YPosition = 2.0F;

            Assert.IsTrue(c.XPosition == 1.0F, "Initial position is not right");
            Assert.IsTrue(c.YPosition == 2.0F, "Initial position is not right");

            c.Scale(0.5F);
            Assert.IsTrue(c.Height == 7.5F / 2.0F, "Scaled height not right");
            Assert.IsTrue(c.Width == 5.0, "Scaled width is not right");

            Assert.IsTrue(c.SGNodeList.Count == 1, "Count of nodes is not 1!");
            Assert.IsTrue(c.SGNodeList[0].FileName == f.FullName, "File name of rendered file is not rigth!");

            Assert.IsTrue(c.SGNodeList[0].ZOrder == 1, "ZIndex is not zero!");
            c.IncZIndex();
            Assert.IsTrue(c.SGNodeList[0].ZOrder == 2, "ZIndex is not one!");
        }

        [TestMethod]
        public void LoadOfSmallFile()
        {
            FileInfo f = new FileInfo("..\\..\\..\\t_DTBuilderLib\\aspectweirdness.jpg");
            Assert.IsTrue(f.Exists, "Image file not found");
            VisualSingleImage_Accessor c = new VisualSingleImage_Accessor(f, 300);

            Assert.IsTrue(Math.Abs(c.Height - 3.78) < 0.1, "Height is not correct");
            Assert.IsTrue(Math.Abs(c.Width - 5.04) < 0.1, "Width is not correct");
            Assert.IsTrue(c.Width / c.Height - 1.33 < 0.1, "Aspect ratio is messed up!");
        }

        [TestMethod]
        public void TestSingleImageEnumerator()
        {
            FileInfo f = new FileInfo("..\\..\\..\\t_DTBuilderLib\\aspectweirdness.jpg");
            Assert.IsTrue(f.Exists, "Image file not found");
            Visual v = new VisualSingleImage(f, 600);

            Assert.AreEqual(1, v.Count(), "There should be only one image that comes back from the enumerator");
        }

        [TestMethod]
        public void TestSimpleName()
        {
            FileInfo f = new FileInfo("..\\..\\..\\t_DTBuilderLib\\aspectweirdness.jpg");
            Assert.IsTrue(f.Exists, "Image file not found");
            Visual v = new VisualSingleImage(f, 600);

            Assert.IsFalse(v.ShortNameGood, "Short name hasn't been set - should be false!");
            v.ShortName = "";
            Assert.IsFalse(v.ShortNameGood, "Short name was set to be blank - shouldn't be good!");
            v.ShortName = null;
            Assert.IsFalse(v.ShortNameGood, "Short name was set to be null - shouldn't be good!");
            v.ShortName = "bogus";
            Assert.AreEqual("bogus", v.ShortName, "Short name was set to be null - shouldn't be good!");
            Assert.IsTrue(v.ShortNameGood, "Short name should be good now");
        }

        [TestMethod]
        public void TestTitle()
        {
            FileInfo f = new FileInfo("..\\..\\..\\t_DTBuilderLib\\aspectweirdness.jpg");
            Assert.IsTrue(f.Exists, "Image file not found");
            Visual v = new VisualSingleImage(f, 600);

            Assert.IsFalse(v.TitleGood, "Short name hasn't been set - should be false!");
            v.Title = "";
            Assert.IsFalse(v.TitleGood, "Short name was set to be blank - shouldn't be good!");
            v.Title = null;
            Assert.IsFalse(v.TitleGood, "Short name was set to be null - shouldn't be good!");
            v.Title = "bogus";
            Assert.AreEqual("bogus", v.Title, "Short name was set to be null - shouldn't be good!");
            Assert.IsTrue(v.TitleGood, "Short name should be good now");
        }

        [TestMethod]
        public void TestIncZ()
        {
            FileInfo f = new FileInfo("..\\..\\..\\t_DTBuilderLib\\aspectweirdness.jpg");
            Visual si = new VisualSingleImage(f, 600);

            Assert.AreEqual(1, si.ZIndex, "The z index should be zero!");
            si.IncZIndex();
            Assert.AreEqual(2, si.ZIndex, "The z index should be one!");
        }
    }
}
